package com.qqtop10.manager.service.impl;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qqtop10.manager.dao.FloorMapper;
import com.qqtop10.manager.model.Category;
import com.qqtop10.manager.model.Floor;
import com.qqtop10.manager.model.temp.FloorTempData;
import com.qqtop10.manager.model.temp.Goods;
import com.qqtop10.manager.service.CategoryService;
import com.qqtop10.manager.service.FloorService;
import com.qqtop10.manager.service.GoodsService;

@Service("floorService")
public class FloorServerImpl implements FloorService {

	@Autowired
	private FloorMapper floorMapper;

	@Autowired
	private CategoryService categoryService;

	@Autowired
	private GoodsService goodsService;

	@Override
	public int insertFloor(Floor floor) {
		return floorMapper.insertFloor(floor);
	}

	@Override
	public int updateFloor(Floor floor) {
		return floorMapper.updateFloor(floor);
	}

	@Override
	public Floor selectFloorById(Integer id) {
		return floorMapper.selectFloorById(id);
	}

	@Override
	public List<FloorTempData> selectFloorAll() {
		List<Floor> floors = floorMapper.selectFloorAll();
		List<FloorTempData> datas = new LinkedList<>();
		for (Floor floor : floors) {
			FloorTempData temp = new FloorTempData();
			// 楼层ID
			int floorId = floor.getFloor_id();
			// 获取已有的分类信息
			String categorysUUID = floor.getFloor_category_uuid();
			temp.setCategoryUuids(categorysUUID.split(","));
			String[] categorys = temp.getCategoryUuids();
			String[] categoryText = new String[categorys.length];
//			Map<String, String> categoryMap = new LinkedHashMap<>();

			for (int i = 0; i < categorys.length; i++) {
				String c = categorys[i];
				if (c == null || c.length() < 32) {
					categoryText[i] = "请选择分类";
					continue;
				}
				Category category = categoryService.selectByUuid(c);
				categoryText[i] = category.getName();
				if(category!=null){
					categoryText[i] = category.getName();
				}else{
					categoryText[i] = "请选择";
				}
				
			}

			// 获取已有的商品信息
			String goodsUUID = floor.getFloor_goods_uuid();
			temp.setGoodsUuids(goodsUUID.split(","));
			String[] goodses = temp.getGoodsUuids();
			String[] goodsText = new String[goodses.length];
//			Map<String, String> goodsMap = new LinkedHashMap<>();

			for (int i = 0; i < goodses.length; i++) {
				String g = goodses[i];
				if (g == null || g.length() < 32) {
					goodsText[i] = "请选择商品";
//					goodsMap.put("0" + i, "请选择商品");
					continue;
				}
				Goods goods = goodsService.selectGoodsByUUID(g);
				goodsText[i] = goods.getName();
				//goodsMap.put(g, goods.getName());
				if(goods!=null){
					goodsText[i] = goods.getName();
				}else{
					goodsText[i] = "请选择";
				}
				
			}

			temp.setFloorId(floorId);
			temp.setCategoryText(categoryText);
			temp.setGoodsText(goodsText);
//			temp.setCategory(categoryMap);
//			temp.setGoods(goodsMap);
			datas.add(temp);
		}
		return datas;
	}

	@Override
	public int deletedFloorByNum(int floor_id) {
		return floorMapper.deletedFloorByNum(floor_id);
	}

}
